/*
 *  Arrow
 *
 *  Created by David Giovannini on 1/5/07.
 *  Copyright 2007 Software by Jove. All rights reserved.
 *
 */
 
#ifndef SBJ_Arrow_Publication_h
#define SBJ_Arrow_Publication_h

#include "SBJ/Arrow/Topic.h"
#include "SBJ/Interfaces/IExceptionObserver.h"

#include <boost/noncopyable.hpp>
#include <boost/thread/mutex.hpp>

#include <hash_map>

namespace SBJ
{

namespace Arrow
{

class Message;
class MsgProxy;

class Publication : private boost::noncopyable
{
public:
	Publication( const std::string& key, const IExceptionObserver::Ptr& excObserver = IExceptionObserver::Ptr() );
	
	virtual ~Publication( void );
	
	inline const std::string& getKey( void ) const;
	
	template< typename MsgType >
	void publish( void );
	
	template< typename MsgType, typename Allocator >
	void publishWithAlloc( const Allocator& allocator );
	
	template< typename MsgType >
	Topic makeTopic( const std::string& topicKey );
	
	inline Topic makeTopic( const std::string& topicKey );
	
	template< typename MsgType >
	Topic getTopic( const std::string& topicKey = std::string() ) const;
	
	Topic getTopic( const std::string& topicKey = std::string() ) const;

private:
	typedef stdext::hash_map< std::string, MsgProxy* > KeyedMsgProxys;
	typedef stdext::hash_map< std::string, Topic::SharedData* > KeyedTopics;
	
	Topic makeTopic( const MsgProxy* MsgProxy, const std::string& topicKey );
	
	const MsgProxy* getMsgProxy( void ) const;
	
	template< typename MsgType >
	const MsgProxy* getMsgProxy( void ) const;
	
	mutable boost::mutex m_guard;
	std::string m_key;
	IExceptionObserver::Ptr m_excObserver;
	KeyedMsgProxys m_msgProxys;
	KeyedTopics m_topics;
};

}

}

#include "SBJ/Arrow/Publication.inl"

#endif